package com.tianqu.tms.www.attn.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.IntegralcardUpdateHistory;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.attn.viewhelper.AttnIntegralViewHelper;
import com.tianqu.tms.www.member.viewhelper.MemberIntegralViewHelper;

/**
 * @author zhangxueliang
 * 经办人积分详细
 */
public class AttnIntegralDetailController extends TmsController{
	private Logger logger = Logger.getLogger(AttnIntegralDetailController.class);
	
	private static final String ATTNID = "attnId";// 会员ID
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			AttnIntegralViewHelper viewhelper = new AttnIntegralViewHelper();
			String attnId = (String) uc.getAttribute(ATTNID);
			
			//
			IntegralRegister integralRegister = IntegralRegister.getInstance();
			Attn attn = Attn.getInstance();
			integralRegister = integralRegister.findIntegralByMemId(attnId);
			if(integralRegister != null){
				//viewhelper.setPriceAll(integralRegister.getPriceAll());
				viewhelper.setTansferPriceAll(integralRegister.getTransferPriceAll());
				viewhelper.setTransferAll(integralRegister.getTransferTime());
				//已用积分总数
				viewhelper.setUsedIntegralAll(integralRegister.getUsedIntegral());
			}		
			attn = attn.findAttnByAttnId(attnId);
			if(attn != null){
				viewhelper.setAccessCardId(attn.getAccessCardId());
				viewhelper.setAccessCardNo(attn.getAccessCardNo());
				//累计积分(分)
				//viewhelper.setCardScore(attn.getCardScore());
				viewhelper.setCardScore(attn.getCardScore()+integralRegister.getUsedIntegral());
				viewhelper.setAttnId(attnId);
				//剩余积分总数
				//viewhelper.setSurplusIntegralAll(attn.getCardScore() - integralRegister.getUsedIntegral());
				viewhelper.setSurplusIntegralAll(attn.getCardScore());
				viewhelper.setVirtualMargin(attn.getVirtualMargin()+"");
				viewhelper.setCardScore(attn.getCardScore());
				viewhelper.setIntegralRatio(attn.getIntegralRatio() * 100 +"");
				
			}
			IntegralcardUpdateHistory history = IntegralcardUpdateHistory.getInstance();
			List historyList = history.findHistoryByMemId(attnId);
			viewhelper.setIntegralcardHistoryList(historyList);
			
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		
	}

}
